
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 0847
 * @Author : WangRuoyu 
 * @Date : 2023/5/21 20:43
 */

#include "iostream"
#include "queue"

using namespace std;

const int N = 1e5 + 10;
const int M = 1e5 + 10;
int h[N];
int e[M];
int ne[M];
int idx = 0;
int n, m;
bool st[N];

struct node {
    int id;
    int step;
};

void add(int a, int b) {
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

int main() {
    scanf("%d %d", &n, &m);

    for (int &i: h) {
        i = -1;
    }

    for (int i = 0; i < m; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        add(a, b);
    }


    int res = -1;
    queue<node> q;
    q.push(node{1, 0});
    st[1] = true;
    while (!q.empty()) {
        node p = q.front();
        q.pop();
        if (p.id == n) {
            res = p.step;
            break;
        }
        for (int i = h[p.id]; i != -1; i = ne[i]) {
            int j = e[i];
            if (!st[j]) {
                q.push(node{j, p.step + 1});
                st[j] = true;
            }
        }
    }

    printf("%d", res);

    return 0;
}